package com.jiutong.meipengdai.ui.activitys;

import android.content.Context;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.jiutong.meipengdai.R;
import com.jiutong.meipengdai.base.BaseActivity;
import com.jiutong.meipengdai.base.CommonAdapter;
import com.jiutong.meipengdai.base.ViewHolder;
import com.jiutong.meipengdai.entity.CouponsBean;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 我的卡券
 * Created by suxi on 2017/6/1.
 */

public class CouponsListActivity extends BaseActivity {

    @Bind(R.id.iv_back)
    ImageView ivBack;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.common_listview)
    ListView lvCoupons;
    private CouponsListAdapter couponsListAdapter;

    @Override
    public int getLayoutResouceId() {
        return R.layout.activity_house_list;
    }

    @Override
    protected void initView() {
        super.initView();
        ButterKnife.bind(this);

        tvTitle.setText("我的卡券");

        ArrayList<CouponsBean> couponsList = new ArrayList<>();

        for(int i = 0; i<10; i++){
            CouponsBean couponsBean = new CouponsBean();
            couponsBean.setNum(i + "");
            couponsBean.setTitle(i+"天免息");
            couponsBean.setDataLine("有效期" + i + "天");
            if(i%3 == 1){
                couponsBean.setIsOverTime("1");

            }else if(i%3 ==2){
                couponsBean.setIsOverTime("2");
            }else{
                couponsBean.setIsOverTime("0");
            }
            couponsList.add(couponsBean);
        }

        couponsListAdapter = new CouponsListAdapter(mContext, couponsList, R.layout.view_coupons_item);
        lvCoupons.setAdapter(couponsListAdapter);
        lvCoupons.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            }
        });

    }

    @OnClick(R.id.iv_back)
    public void onViewClicked(View view) {
        switch (view.getId()){
            case R.id.iv_back:
                finish();
                break;
        }
    }

    public class CouponsListAdapter extends CommonAdapter <CouponsBean>{

        public CouponsListAdapter(Context context, List<CouponsBean> listDatas, int layoutId) {
            super(context, listDatas, layoutId);
        }

        @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
        @Override
        protected void fillData(ViewHolder holder, int position) {
            TextView tv_title = holder.getView(R.id.tv_title);
            TextView tv_data_line = holder.getView(R.id.tv_data_line);
            LinearLayout ll_bg = holder.getView(R.id.ll_bg);
            TextView tv_count = holder.getView(R.id.tv_count);

            CouponsBean couponsBean = listDatas.get(position);
            if("1".equals(couponsBean.getIsOverTime())){
                ll_bg.setBackground(getDrawable(R.mipmap.coupons_used_icon));
            }else if("2".equals(couponsBean.getIsOverTime())){
                ll_bg.setBackground(getDrawable(R.mipmap.coupons_noused_icon));
            }else{
                ll_bg.setBackground(getDrawable(R.mipmap.coupons_overtime_icon));
            }

            tv_count.setText(couponsBean.getNum());
            tv_data_line.setText(couponsBean.getDataLine());
            tv_title.setText(couponsBean.getTitle());
        }
    }
}
